package com.jiyun.model.day08;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author yangYingJie
 * @Date 2023/9/7 19：39
 **/

@Data
@Document(collection = "remark")
public class Remark {

    @Id
    private String id;
    //课程id
    private Integer cid;
    //用户id
    private Integer mid;
    //用户名称
    private String mname;
    //父子级
    private String pid;
    //选定评论名称
    private String pname;
    //内容
    private String name;
    //评论时间
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "Asia/Shanghai")
    private Date startTime;
    //点赞数
    private Integer likes;
    //回复数
    private Integer reply;
    @Transient
    @TableField(exist = false)
    private List<Remark> children;

    public void addChild(Remark child) {
        if (children == null) {
            children = new ArrayList<>();
        }
        children.add(child);
    }

    public void generateComments(List<Remark> comments, String parentId) {
        List<Remark> childComments = comments.stream()
                .filter(comment -> comment.getPid().equals(parentId))
                .collect(Collectors.toList());

        for (Remark childComment : childComments) {
            generateComments(comments, childComment.getId());
            addChild(childComment);
        }
    }


}
